package com.st.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.common.enums.UserStatus;
import com.st.controller.command.SysUserCommand;
import com.st.common.convert.SysUserConvert;
import com.st.domain.SysRole;
import com.st.domain.SysUser;
import com.st.mapper.SysUserMapper;
import com.st.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author shent
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private final SysUserMapper sysUserMapper;
    @Autowired
    SysUserConvert sysUserConvert;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(SysUserCommand command) {
        checkUnique(command);
        SysUser sysUser = sysUserConvert.commandToDomain(command);
        sysUser.setPassword(bCryptPasswordEncoder.encode(command.getPassword()));
        sysUserMapper.insert(sysUser);
        return sysUser.getId();
    }

    private void checkUnique(SysUserCommand command) {

    }

    @Override
    public SysUser getByUsername(String username) {
        return sysUserMapper.selectByUsername(username);
    }

    @Override
    public List<SysRole> getRolesById(Long id) {
        return sysUserMapper.selectRolesById(id);
    }

    @Override
    public List<String> getResourceCodesByUserId(Long id) {
        return sysUserMapper.selectResourcesCodeByUserId(id);
    }


}